import axios from 'axios'
 
// 创建axios实例
const service = axios.create({
  baseURL: import.meta.env.BASE_URL, // api的base_url
  timeout: 1200000 // 请求超时时间
});
 
// 请求拦截器
service.interceptors.request.use(
  config => {
    return config
  },
  error => {
    Promise.reject(error)
  }
);
 
// 响应拦截器
service.interceptors.response.use(
  response => {
    const res = response
    return res
  },
  error => {
    return Promise.reject(error)
  }
)

const request = ({ method, url, data, headers={}, responseType }) => {
  data = method === 'get' ? { params: data } : data
  // 发送请求
  return new Promise((resolve, reject) => {
    service({
      method: method || 'get',
      url,
      data,
      responseType,
      headers
    })
      .then(response => {
        resolve(response)
      })
      .catch(error  => {
        reject(error)
      })
  })
  .catch(error  => {
    console.log(error)
  })
}

export default request